package com.yunwaikeji.gy_tool_library.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.Gravity;
import android.widget.RelativeLayout;

import com.yunwaikeji.gy_tool_library.R;
import com.yunwaikeji.gy_tool_library.utils.display.GYDisplayUtils;
import com.yunwaikeji.gy_tool_library.info.GYTextViewInfo;
import com.yunwaikeji.gy_tool_library.view.GYRelativeLayout;
import com.yunwaikeji.gy_tool_library.view.GYTextView;
import com.yunwaikeji.gy_tool_library.view.GYView;

/**
 * 按钮
 * 郭杨
 * 2019/08/01
 */
public class GYButton extends GYRelativeLayout implements GYTextViewInfo {
    private GYTextView clickTextView;
    //    private View clickableView;
    private GYView maskView;

    private String textString;
    private int textColor = Color.BLACK;
    private int textSizePx = 40;
    //    private boolean clickable = true;
    private boolean isMask = false;
    private int maskColor = Color.parseColor("#70FFFFFF");
    private int clickableColor = Color.parseColor("#70FFFFFF");
    private int maxLines = 999999999;

    private int drawableWidthPx = -1;
    private int drawableHeightPx = -1;
    private int leftDrawableWidth = -1;
    private int leftDrawableHeight = -1;
    private int topDrawableWidth = -1;
    private int topDrawableHeight = -1;
    private int rightDrawableWidth = -1;
    private int rightDrawableHeight = -1;
    private int bottomDrawableWidth = -1;
    private int bottomDrawableHeight = -1;
    private int drawHorizontalLine = HorizontalLine.HIDE;

    private Drawable leftDrawable;
    private Drawable topDrawable;
    private Drawable rightDrawable;
    private Drawable bottomDrawable;

    private int drawablePadding = 0;
    private boolean isBold = false;

    private Drawable pressDrawable;

    public GYButton(Context context) {
        this(context, null);
    }

    public GYButton(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public GYButton(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(attrs);
    }

    private void init(AttributeSet attributeSet) {
        String hint = "";
        int hintColor = WRAP_CONTENT;
        if (attributeSet != null) {
            TypedArray typedArray = getContext().obtainStyledAttributes(attributeSet, R.styleable.GYButton);
            textString = typedArray.getString(R.styleable.GYButton_android_text);
            textColor = typedArray.getColor(R.styleable.GYButton_android_textColor, textColor);
            pressDrawable = typedArray.getDrawable(R.styleable.GYButton_pressground);
            textSizePx = typedArray.getDimensionPixelSize(R.styleable.GYButton_android_textSize, textSizePx);
//            clickable = typedArray.getBoolean(R.styleable.GYButton_clickable, clickable);
            isBold = typedArray.getBoolean(R.styleable.GYButton_bold, isBold);
            isMask = typedArray.getBoolean(R.styleable.GYButton_isMask, isMask);
            maskColor = typedArray.getColor(R.styleable.GYButton_maskColor, maskColor);
            clickableColor = typedArray.getColor(R.styleable.GYButton_clickableColor, clickableColor);
            maxLines = typedArray.getInt(R.styleable.GYButton_maxLines, maxLines);
            drawablePadding = typedArray.getDimensionPixelSize(R.styleable.GYButton_android_drawablePadding, drawablePadding);
            drawableWidthPx = typedArray.getDimensionPixelSize(R.styleable.GYButton_drawableWidth, drawableWidthPx);
            drawableHeightPx = typedArray.getDimensionPixelSize(R.styleable.GYButton_drawableHeight, drawableHeightPx);
            leftDrawableWidth = typedArray.getDimensionPixelSize(R.styleable.GYButton_leftDrawableWidth, leftDrawableWidth);
            leftDrawableHeight = typedArray.getDimensionPixelSize(R.styleable.GYButton_leftDrawableHeight, leftDrawableHeight);
            topDrawableWidth = typedArray.getDimensionPixelSize(R.styleable.GYButton_topDrawableWidth, topDrawableWidth);
            topDrawableHeight = typedArray.getDimensionPixelSize(R.styleable.GYButton_topDrawableHeight, topDrawableHeight);
            rightDrawableWidth = typedArray.getDimensionPixelSize(R.styleable.GYButton_rightDrawableWidth, rightDrawableWidth);
            rightDrawableHeight = typedArray.getDimensionPixelSize(R.styleable.GYButton_rightDrawableHeight, rightDrawableHeight);
            bottomDrawableWidth = typedArray.getDimensionPixelSize(R.styleable.GYButton_bottomDrawableWidth, bottomDrawableWidth);
            bottomDrawableHeight = typedArray.getDimensionPixelSize(R.styleable.GYButton_bottomDrawableHeight, bottomDrawableHeight);
            leftDrawable = typedArray.getDrawable(R.styleable.GYButton_leftDrawable);
            topDrawable = typedArray.getDrawable(R.styleable.GYButton_topDrawable);
            rightDrawable = typedArray.getDrawable(R.styleable.GYButton_rightDrawable);
            bottomDrawable = typedArray.getDrawable(R.styleable.GYButton_bottomDrawable);
            drawHorizontalLine = typedArray.getInt(R.styleable.GYButton_drawHorizontalLine, drawHorizontalLine);
            hint = typedArray.getString(R.styleable.GYButton_android_hint);
            hintColor = typedArray.getColor(R.styleable.GYButton_android_textColorHint, hintColor);
            typedArray.recycle();
        }

        if (drawableWidthPx >= 0) {
            leftDrawableWidth = drawableWidthPx;
            topDrawableWidth = drawableWidthPx;
            rightDrawableWidth = drawableWidthPx;
            bottomDrawableWidth = drawableWidthPx;
        }
        if (drawableHeightPx >= 0) {
            leftDrawableHeight = drawableHeightPx;
            topDrawableHeight = drawableHeightPx;
            rightDrawableHeight = drawableHeightPx;
            bottomDrawableHeight = drawableHeightPx;
        }

//        LayoutInflater.from(getContext()).inflate(R.layout.view_gy_button, this);

        clickTextView = new GYTextView(getContext());
        clickTextView.setHint(hint);
        if (hintColor != WRAP_CONTENT) {
            clickTextView.setHintTextColor(hintColor);
        }
        clickTextView.setClickable(false);
//        clickableView = new View(getContext());
//        clickableView.setClickable(true);
        maskView = new GYView(getContext());
        maskView.setClickable(true);

        getViewSet().addViews(clickTextView, maskView);

        LayoutParams layoutParams = (LayoutParams) clickTextView.getLayoutParams();
        layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE);
        clickTextView.setLayoutParams(layoutParams);

//        handler.post(runnable);

//        setClickable1(clickable);

        if (getBackgroundBuilder().getChangeground() == null) {
            if (pressDrawable == null) {
                pressDrawable = new ColorDrawable(0xFFF2F2F2);
            }
            getBackgroundBuilder().setChangeground(pressDrawable).build();
        }

        initView();
        setMask(isMask);
        initDrawable();
        initDrawLine();
        setBold(isBold);
    }

    public GYButton setPress(Drawable pressDrawable) {
        this.pressDrawable = pressDrawable;
        getBackgroundBuilder().setChangeground(pressDrawable).build();
        return this;
    }

    public GYButton setPressColor(int color) {
        return setPress(new ColorDrawable(color));
    }

    private void initView() {
        clickTextView.setText(textString);
        clickTextView.setGravity(Gravity.CENTER);
        clickTextView.setTextColor(textColor);
        clickTextView.setTextSize(GYDisplayUtils.px2sp(getContext(), Math.round(textSizePx)));
        clickTextView.setMaxLines(maxLines);
        clickTextView.setCompoundDrawablePadding(drawablePadding);

        setMaskColor(maskColor);
    }

    private void initDrawLine() {
        int flags = 0;
        switch (drawHorizontalLine) {
            case HorizontalLine.HIDE:
                flags = 0;
                break;
            case HorizontalLine.CENTER:
                flags = Paint.STRIKE_THRU_TEXT_FLAG | Paint.ANTI_ALIAS_FLAG;
                break;
            case HorizontalLine.BOTTOM:
                flags = Paint.UNDERLINE_TEXT_FLAG | Paint.ANTI_ALIAS_FLAG;
                break;
            case HorizontalLine.ALL:
                flags = Paint.STRIKE_THRU_TEXT_FLAG | Paint.UNDERLINE_TEXT_FLAG | Paint.ANTI_ALIAS_FLAG;
                break;
        }
        clickTextView.getPaint().setFlags(flags);//设置中划线
    }

    private GYButton initDrawable() {
        if (leftDrawable != null) {
            leftDrawable.setBounds(0, 0, leftDrawableWidth >= 0 ? leftDrawableWidth : leftDrawable.getIntrinsicWidth(), leftDrawableHeight >= 0 ? leftDrawableHeight : leftDrawable.getIntrinsicHeight());
        }
        if (topDrawable != null) {
            topDrawable.setBounds(0, 0, topDrawableWidth >= 0 ? topDrawableWidth : topDrawable.getIntrinsicWidth(), topDrawableHeight >= 0 ? topDrawableHeight : topDrawable.getIntrinsicHeight());
        }
        if (rightDrawable != null) {
            rightDrawable.setBounds(0, 0, rightDrawableWidth >= 0 ? rightDrawableWidth : rightDrawable.getIntrinsicWidth(), rightDrawableHeight >= 0 ? rightDrawableHeight : rightDrawable.getIntrinsicHeight());
        }
        if (bottomDrawable != null) {
            bottomDrawable.setBounds(0, 0, bottomDrawableWidth >= 0 ? bottomDrawableWidth : bottomDrawable.getIntrinsicWidth(), bottomDrawableHeight >= 0 ? bottomDrawableHeight : bottomDrawable.getIntrinsicHeight());
        }
        clickTextView.setCompoundDrawables(leftDrawable, topDrawable, rightDrawable, bottomDrawable);
        return this;
    }

    public GYButton setLeftDrawable(Drawable leftDrawable) {
        this.leftDrawable = leftDrawable;
        return this;
    }

    public GYButton setLeftDrawableSize(int leftDrawableWidth, int leftDrawableHeight) {
        this.leftDrawableWidth = leftDrawableWidth;
        this.leftDrawableHeight = leftDrawableHeight;
        return initDrawable();
    }

    public GYButton setLeftDrawableWidth(int leftDrawableWidth) {
        return setLeftDrawableSize(leftDrawableWidth, leftDrawableHeight);
    }

    public GYButton setLeftDrawableHeight(int leftDrawableHeight) {
        return setLeftDrawableSize(leftDrawableWidth, leftDrawableHeight);
    }

    public GYButton setTopDrawable(Drawable topDrawable) {
        this.topDrawable = topDrawable;
        return this;
    }

    public GYButton setTopDrawableSize(int topDrawableWidth, int topDrawableHeight) {
        this.topDrawableWidth = topDrawableWidth;
        this.topDrawableHeight = topDrawableHeight;
        return initDrawable();
    }

    public GYButton setTopDrawableWidth(int topDrawableWidth) {
        return setTopDrawableSize(topDrawableWidth, topDrawableHeight);
    }

    public GYButton setTopDrawableHeight(int topDrawableHeight) {
        return setTopDrawableSize(topDrawableWidth, topDrawableHeight);
    }

    public GYButton setRightDrawable(Drawable rightDrawable) {
        this.rightDrawable = rightDrawable;
        return this;
    }

    public GYButton setRightDrawableSize(int rightDrawableWidth, int rightDrawableHeight) {
        this.rightDrawableWidth = rightDrawableWidth;
        this.rightDrawableHeight = rightDrawableHeight;
        return initDrawable();
    }

    public GYButton setRightDrawableWidth(int rightDrawableWidth) {
        return setRightDrawableSize(rightDrawableWidth, rightDrawableHeight);
    }

    public GYButton setRightDrawableHeight(int rightDrawableHeight) {
        return setRightDrawableSize(rightDrawableWidth, rightDrawableHeight);
    }

    public GYButton setBottomDrawable(Drawable bottomDrawable) {
        this.bottomDrawable = bottomDrawable;
        return this;
    }

    public GYButton setBottomDrawableSize(int bottomDrawableWidth, int bottomDrawableHeight) {
        this.bottomDrawableWidth = bottomDrawableWidth;
        this.bottomDrawableHeight = bottomDrawableHeight;
        return initDrawable();
    }

    public GYButton setBottomDrawableWidth(int bottomDrawableWidth) {
        return setBottomDrawableSize(bottomDrawableWidth, bottomDrawableHeight);
    }

    public GYButton setBottomDrawableHeight(int bottomDrawableHeight) {
        return setBottomDrawableSize(bottomDrawableWidth, bottomDrawableHeight);
    }

    public GYButton setMaxLines(int maxLines) {
        this.maxLines = maxLines;
        clickTextView.setMaxLines(maxLines);
        return this;
    }

    public GYButton setMask(boolean isMask) {
        this.isMask = isMask;
        if (isMask) {
            maskView.setVisibility(VISIBLE);
        } else {
            maskView.setVisibility(GONE);
        }
        return refreshMaskView();
    }

    public boolean isMask() {
        return isMask;
    }

    /**
     * 设置文字大小
     *
     * @param sp
     * @return
     */
    public GYButton setTextSizeSp(float sp) {
        setTextSizePx(GYDisplayUtils.sp2px(getContext(), sp));
        return this;
    }

    /**
     * 设置文字大小
     *
     * @param px
     * @return
     */
    public GYButton setTextSizePx(int px) {
        textSizePx = px;
        clickTextView.setTextSize(GYDisplayUtils.px2sp(getContext(), Math.round(textSizePx)));
        return this;
    }

    /**
     * 设置文字
     *
     * @param string
     * @return
     */
    public GYButton setText(String string) {
        textString = string;
        clickTextView.setText(string);
        return this;
    }

    /**
     * 设置文字
     * <p>
     * //     * @param string
     *
     * @return
     */
    public GYButton setText(Object object) {
        if (object == null) {
            return setText("");
        }
        return setText(String.valueOf(object));
    }

    /**
     * 设置文字颜色
     *
     * @param color
     * @return
     */
    public GYButton setTextColor(int color) {
        textColor = color;
        clickTextView.setTextColor(color);
        return this;
    }

    public GYButton setDrawHorizontalLine(int drawHorizontalLine) {
        this.drawHorizontalLine = drawHorizontalLine;
        initDrawLine();
        return this;
    }

    public int getDrawHorizontalLine() {
        return drawHorizontalLine;
    }

    public GYButton setMaskColor(int maskColor) {
        this.maskColor = maskColor;
        return refreshMaskView();
    }

    public GYButton refreshMaskView() {
        maskView.setBackground(GYDisplayUtils.newBuilder()
                .setNormalColor(maskColor)
                .setRadiusLeftTopPx(getBackgroundBuilder().getRadiusLeftTopPx())
                .setRadiusRightTopPx(getBackgroundBuilder().getRadiusRightTopPx())
                .setRadiusRightBottomPx(getBackgroundBuilder().getRadiusRightBottomPx())
                .setRadiusLeftBottomPx(getBackgroundBuilder().getRadiusLeftBottomPx())
                .buildDrawable());
        return this;
    }

    public int getMaskColor() {
        return maskColor;
    }

    public int getClickableColor() {
        return clickableColor;
    }

    public GYButton setBold(boolean bold) {
        clickTextView.getViewSet().setBold(bold);
        return this;
    }

    public boolean isBold() {
        return clickTextView.getViewSet().isBold();
    }

    public boolean isTextEmpty() {
        return clickTextView.getViewSet().isTextEmpty();
    }

    public boolean isHintEmpty() {
        return clickTextView.getViewSet().isHintEmpty();
    }

    public String getTextString() {
        return clickTextView.getViewSet().getTextString();
    }

    public CharSequence getText() {
        return clickTextView.getText();
    }

    public GYTextView getTextView() {
        return clickTextView;
    }

    public GYView getMaskView() {
        return maskView;
    }
}